package com.x.gzcrm.web.controller;


import com.x.gzcrm.model.*;
import com.x.gzcrm.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/permission")
public class PermissionController {

    @Autowired
    IPermissionService iPermissionService;

    @RequestMapping(value = "/list")
    public String list(){
        return "permission/list";
    }


    @RequestMapping(value = "/add")
    public String add(Model model){
        //获取父功能节点数据
        List<Permission> parentMenus = iPermissionService.findAllParentMenus();
        model.addAttribute("parentMenus",parentMenus);
        return "permission/add";
    }
    //删除
    @RequestMapping("delete")
    @ResponseBody
    public CURDResult delete(Integer permisssionId){
        CURDResult result = new CURDResult();
        iPermissionService.deleteByPermissionId(permisssionId);
        return result;
    }

    //更新
    @RequestMapping("edit")
    public String edit(Model model,Integer permisssionId){
        System.out.println("修改权限的id=" + permisssionId);
        Permission permission = iPermissionService.findPermissionById(permisssionId);
        //获取父功能节点数据
        List<Permission> parentMenus = iPermissionService.findAllParentMenus();
        model.addAttribute("parentMenus",parentMenus);
        model.addAttribute("permission",permission);
        System.out.println("查询的id的内容"+permission+"---"+parentMenus);
        return "permission/edit";
    }


    @RequestMapping("listJson")
    @ResponseBody
    /**
     * page 显示的当前页
     * limit 每次显示多少条
     */
    public PageResult<Permission> listJson(Permission condition){

        System.out.println(condition);
        PageResult<Permission> result = new PageResult<>();
        result.setData(iPermissionService.findAllPermission());

        return result;
    }

    @RequestMapping("save")
    @ResponseBody
    public CURDResult save(Permission permission){
        CURDResult result = new CURDResult();// {success:1,msg:””}
        System.out.println(permission);
        //service保存
        System.out.println("增加权限成功");
        iPermissionService.insert(permission);
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    public CURDResult update(Permission permission){
        CURDResult result = new CURDResult();// {success:1,msg:””}
        System.out.println(permission);
        //service保存
        System.out.println("修改权限成功");
        iPermissionService.update(permission);
        return result;
    }
}
